
/****************************************************************************************
*                                                                                      *
*                          Projeto BioInfo - Lincs and CIn / UFPE                      *
*                                    07/10/2014                                        *
*																					   *
****************************************************************************************
* Descricao: Arquivo que contem funcao run_canonic                             *
****************************************************************************************
* Responsaveis: Jefferson Ramos(jrla)													   *
*                                                                                      */
/*                                                                                     *
****************************************************************************************/

#include <bioaplication_sw.h>

void bioaplication_sw::run_canonic(){


	fopen_s(&bioinfo_canonic->TaxaOut, "../files/canonic_TaxaOut.txt", "w+");

	printf("\nPress ENTER to run Canonic:\n");
	fflush(stdin);
	getchar();

	fopen_s(&bioinfo_canonic->BD_nucleotideos, "../files/BD_out_bin.bin", "rb");
	if (bioinfo_canonic->BD_nucleotideos == NULL){
		printf("\nError: No open file canonic_BD_nucleotideos\n");
		fflush(stdin);
		getchar();
	}

	int m, i, j;
	num_individuals = bioinfo_canonic->get_num_individuals();
	int max_individuals = num_individuals;
	int alocation_max_ind = 200000;

	m = num_individuals / alocation_max_ind;

	if ( num_individuals >= alocation_max_ind )
		num_individuals = alocation_max_ind;
	else
		alocation_max_ind = num_individuals;

	//passando numero maximo de individuos para objeto do Canonic
	bioinfo_canonic->set_num_individuals( alocation_max_ind );

	// Vectors Allocation
	if (bioinfo_canonic->vectors_malloc()){ printf("ERROR: vectors_malloc\n"); exit(-1); }

	// Read Input Query Files and Rewind File
	bioinfo_canonic->read_input_query();
	rewind( bioinfo_canonic->BD_nucleotideos );

	printf("Executando Canonic: %d rodada(s)\n\n", run_times);

	bioinfo_canonic->time_average = 0.0;
	for (j = 0; j < run_times; j++){
		bioinfo_canonic->sum_time = 0.0;

		//passando numero maximo de individuos para objeto do Canonic
		bioinfo_canonic->set_num_individuals(alocation_max_ind);

		for (i = 0; i < m; i++){

			// Threads multiplo dos individuos (Get MMC)
			qtd_alignment *= alignment_by_individuals*alocation_max_ind;

			//passando alignment_by_threads para objeto do Threads
			bioinfo_canonic->set_alignment_by_threads(qtd_alignment);

			bioinfo_canonic->read_input_individuals(); // Read Input Individuals Files

			tt.Tic();
			bioinfo_canonic->run(); // Run NeedleMan Canonic and Threads
			bioinfo_canonic->sum_time += tt.Toc();

#ifdef PRINT_TAXA// Taxa Output Files
			bioinfo_canonic->print_results();
#endif
		}
		if (( max_individuals - alocation_max_ind*i ) > 0)
		{
			//passando numero maximo de individuos para objeto do Canonic
			bioinfo_canonic->set_num_individuals( max_individuals - alocation_max_ind*m );


			// Threads multiplo dos individuos restantes (Get MMC)
			qtd_alignment *= alignment_by_individuals*(max_individuals - alocation_max_ind*m);

			//passando alignment_by_threads para objeto do Threads
			bioinfo_canonic->set_alignment_by_threads(qtd_alignment);

			bioinfo_canonic->read_input_individuals(); // Read Input Individuals Files

			tt.Tic();
			bioinfo_canonic->run(); // Run NeedleMan Canonic and Threads
			bioinfo_canonic->sum_time += tt.Toc();

#ifdef PRINT_TAXA// Taxa Output Files
			bioinfo_canonic->print_results();
#endif
		}
	}
	bioinfo_canonic->time_average += bioinfo_canonic->sum_time;
	printf("Rodada: %d Tempo: %.2f ms\n", j + 1, bioinfo_canonic->sum_time * 1000);

	rewind( bioinfo_canonic->BD_nucleotideos );
#ifdef PRINT_TAXA
	rewind( bioinfo_canonic->TaxaOut );
#endif

	fprintf(bioinfo_canonic->Results, "Canonic - NUM_INDIVIDUALS: %d\n", max_individuals);
	fprintf(bioinfo_canonic->Results, "Tempo medio: %.2f ms\n\n", bioinfo_canonic->time_average * 1000 / run_times);
	printf("Tempo medio: %.2f ms\n\n", bioinfo_canonic->time_average * 1000 / run_times);

	printf("\nPress ENTER to exit Canonic:");
	fflush(stdin);
	getchar();

	bioinfo_canonic->set_num_individuals( max_individuals );//restaurando num_individuals

	fclose( bioinfo_canonic->BD_nucleotideos );//fechando arquivo BD
	fclose(bioinfo_canonic->TaxaOut);
}

